package org.zpmis.api.business;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.domain.contract.service.ContractService;
import org.zpmis.domain.contract.vo.ContractCountReceived;
import org.zpmis.domain.contract.vo.ContractReceived;
import org.zpmis.domain.order.qo.StatisticsQo;
import org.zpmis.domain.order.service.OrderService;
import org.zpmis.domain.order.vo.OrderCountReceived;
import org.zpmis.domain.order.vo.OrderReceived;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with CodeGenerator
 * Description:统计分析业务处理
 * @author  Yang
 * Date: 2021-6-2
 * Time: 14:53:29
 */
@Service
@Slf4j
public class StatisticsWebService extends BaseWebService {

     @Reference
     private ContractService contractService;

     @Reference
     private OrderService orderService;

    /**
     * 订单回款柱状图统计
     * @param statisticsQo
     */
    public List<List<String>> orderHistogramStatistics(StatisticsQo statisticsQo) throws BusinessException,Exception {
        List<List<String>> resultList = new ArrayList<>();
        resultList.add(Arrays.asList("product","订单应收款","订单已收款"));
        List<OrderReceived> orderReceived = orderService.getOrderReceived(statisticsQo.getStartTime(), statisticsQo.getEndTime());
        if(orderReceived != null && !orderReceived.isEmpty()){
            orderReceived.stream().forEach(recevied -> {
                resultList.add(Arrays.asList(recevied.getDate(), recevied.getTotalAmount(), recevied.getCollection()));
            });
        }
        return resultList;
    }

    /**
     * 账单回款柱状图统计
     * @param statisticsQo
     */
    public List<List<String>> contractHistogramStatistics(StatisticsQo statisticsQo) throws BusinessException,Exception {
        List<List<String>> resultList = new ArrayList<>();
        resultList.add(Arrays.asList("product","账单应收款","账单已收款"));
        List<ContractReceived> orderReceived = contractService.getContractReceived(statisticsQo.getStartTime(), statisticsQo.getEndTime());
        if(orderReceived != null && !orderReceived.isEmpty()){
            orderReceived.stream().forEach(recevied -> {
                resultList.add(Arrays.asList(recevied.getDate(), recevied.getTotalAmount(), recevied.getCollection()));
            });
        }
        return resultList;
    }


    /**
     * 订单数量 按日统计
     * @param statisticsQo
     * @return
     */
    public List<List<String>> orderCountStatistics(StatisticsQo statisticsQo) {
        List<List<String>> resultList = new ArrayList<>();
        resultList.add(Arrays.asList("product","订单数量"));
        List<OrderCountReceived> orderCountReceivedList = orderService.getOrderCountReceived(statisticsQo.getStartTime(), statisticsQo.getEndTime());
        if(orderCountReceivedList != null && !orderCountReceivedList.isEmpty()){
            orderCountReceivedList.stream().forEach(recevied -> {
                resultList.add(Arrays.asList(recevied.getDate(), recevied.getOrderCount()));
            });
        }
        return resultList;
    }

    /**
     * 合同数量 按月统计
     * @param statisticsQo
     * @return
     */
    public Object contractCountStatistics(StatisticsQo statisticsQo) {
        List<List<String>> resultList = new ArrayList<>();
        resultList.add(Arrays.asList("product","合同数量"));
        List<ContractCountReceived> contractCountReceivedList = contractService.getContractCountReceived(statisticsQo.getStartTime(), statisticsQo.getEndTime());
        if(contractCountReceivedList != null && !contractCountReceivedList.isEmpty()){
            contractCountReceivedList.stream().forEach(recevied -> {
                resultList.add(Arrays.asList(recevied.getDate(), recevied.getContractCount()));
            });
        }
        return resultList;
    }
}
